/**
 * Created by pig2guang on 2017/5/18.
 */
$(function () {
    $('#md5_register_form').submit(function () {
        var username = $("#reg_username").val();
        var pwd = $('#reg_password').val();
        var pwd2 = $("#reg_password2").val();
        if(username.length<6 || username.length>20 || pwd.length<6 || pwd.length>20){
            alert("用户名和密码不能低于6位，超过20位！");
            return false;
        }
        if(pwd != pwd2){
            alert("两次输入密码不一致！");
            return false;
        }
        var salt = "{*nbsjt*asar#cdxd#}";
        var md5pwd = $.md5(pwd + salt);
        $.ajax({
            url: 'addMd5User',
            cache: false,
            dateType: 'json',
            contentType: "application/json;charset=utf-8",
            type: "post",
            data: JSON.stringify({
                username: username,
                password: md5pwd,
            }),
            success: function (data) {
                if (data.code == 201) {
                    alert("注册成功!用户：" + data.user.username );
                    window.location.href = "hello";
                }
                else {
                    alert("注册失败！用户已存在！");
                }
            },
            error: function (xhr) {
                alert('servlet出错\n' + xhr.status + '\n' + xhr.responseText);
            }
        });
    })
});
